package com.bob.common.resources.exec;

import com.bob.common.DataStore;
import com.bob.common.bean.Constant;
import com.bob.common.bean.DiffItem;
import com.bob.common.bean.ResourcesRef;
import com.bob.common.resources.ResourcesExec;
import com.bob.common.utils.FileUtil;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.logging.SystemStreamLog;

import java.io.File;
import java.util.List;

/**
 * 处理普通项目文件，比如 java ,资源文件
 * @author heh
 */
public class DefaultExec implements ResourcesExec{
    static Log logger = new SystemStreamLog();
    /**
     *
     * @param baseDir 项目路径
     * @param patchOutPath
     * @param finalName
     */
    @Override
    public void exec(File baseDir, File outputDirectory,File patchOutPath, String finalName){
        List<DiffItem> diffItems = DataStore.getDataStore().getDiffItems();
        // "F:\\dev\\cczyz\\cczyz\\cczyz_web\\target\\patch\\2021-08-07-12-12-54\\cczyz_web-0.0.1-SNAPSHOT
        final String pathPrefix = patchOutPath.getAbsolutePath() +File.separator+ finalName;
        logger.info(String.format("=== 补丁文件根目录  %s  ", pathPrefix));

        diffItems.forEach(diffItem -> {
            File diffItemAbsPath = new File(DataStore.getDataStore().getRootDir()+File.separator+diffItem.getPath());
            for (ResourcesRef resourcesRef: DataStore.getDataStore().getResourcesRefs()) {
                final String s = resourcesRef.getS();
                final String t = resourcesRef.getP();
                if (diffItemAbsPath.getPath().replace(baseDir.getAbsolutePath(),"").startsWith(s)) {
                    String target = diffItemAbsPath.getAbsolutePath().replace(s,t).replace(baseDir.getAbsolutePath(), outputDirectory.getAbsolutePath()+File.separator+finalName);
                    // F:\dev\cczyz\cczyz\cczyz_web\target\cczyz_web-0.0.1-SNAPSHOT\WEB-INF\classes\params.properties
                    File file = new File(target);
                    // 编译后的文件与源文件名称一致 ,比如资源文件
                     if (file.exists()) {
                        File patchDir = new File(pathPrefix+File.separator + file.getParentFile().getAbsolutePath().replace(outputDirectory.getAbsolutePath(), "").replace(finalName,""));
                        if(!patchDir.exists()){
                            FileUtil.mkdir(patchDir);
                        }
                        FileUtil.copy(file,patchDir,true);

                        logger.info(String.format("=== 复制文件到补丁目录成功 ： "));
                        logger.info(String.format("=== \t\t\t\t  %s \t===>>\t %s ", target,patchDir));
                        break;
                     }else{
                        // 不一致的情况
                        // java -> class
                        String type = FileUtil.extName(target).toLowerCase();
                        switch (type){
                            case Constant.FILETYPE_JAVA:
                                FileUtil.copyJavaFileToPatchDir(diffItemAbsPath.getAbsolutePath(),resourcesRef,baseDir,outputDirectory,finalName,new File(pathPrefix));
                                break;
                        }
                    }
                }
            }
        });


    }
}
